package fa7.socialcode.web;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.faces.FactoryFinder;
import javax.faces.application.ApplicationFactory;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fa7.socialcode.model.Comentario;
import fa7.socialcode.model.Conteudo;
import fa7.socialcode.model.Usuario;
import fa7.socialcode.service.ComentarioManager;

public class ComentarioForm {
	private final Log log = LogFactory.getLog(ComentarioForm.class);
	private String id;
	private Comentario comentario = new Comentario();
	private ComentarioManager mgr;
	private Long conteudoId;
	private Long usuarioId;
	private Usuario usuarioInSession;
		
	public Long getUsuarioId() {
		return usuarioId;
	}

	public void setUsuarioId(Long usuarioId) {
		this.usuarioId = usuarioId;
	}

	public Long getConteudoId() {
		return conteudoId;
	}

	public void setConteudoId(Long conteudoId) {
		this.conteudoId = conteudoId;
	}

    private ArrayList<SelectItem> listaDeConteudos = null;
    
	public ArrayList<SelectItem> getListaDeConteudos() {

		listaDeConteudos = new ArrayList<SelectItem>();

		List<Conteudo> conteudos = mgr.findConteudos();

		listaDeConteudos.add(new SelectItem("", "Selecione o Conteudo"));

		for (Conteudo conteudo : conteudos) {
			listaDeConteudos.add(new SelectItem("" + conteudo.getId(),
					conteudo.getTitulo()));
		}

		return listaDeConteudos;
	}
	
	 private ArrayList<SelectItem> listaDeUsuarios = null;
	    
	    public ArrayList<SelectItem> getListaDeUsuarios(){
	    	
	    	listaDeUsuarios = new ArrayList<SelectItem>();
	    	
	    	List<Usuario> usuarios = mgr.findUsuarios();
	    	
	    	listaDeUsuarios.add(new SelectItem("", "Selecione o Usuario"));
	    	
	    	for (Usuario usuario : usuarios){
	    		listaDeUsuarios.add(new SelectItem("" + usuario.getId(), usuario.getNome()));
	    	}
	    	
	    	return listaDeUsuarios;
	    }
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Comentario getComentario() {
		return comentario;
	}

	public void setComentario(Comentario comentario) {
		this.comentario = comentario;
	}

	public void setComentarioManager(ComentarioManager comentarioManager) {
		this.mgr = comentarioManager;
	}

	public String edit() {

		if (id != null) {
			// assuming edit
			setComentario(mgr.findComentario(id));
		}

		return "success";
	}

	public String save() {
		Comentario ex = getComentario();
		if (ex.getId() == 0) {
			ex.setId(null);
		}
		mgr.saveComentario(ex);
		addMessage("comentario.saved", getComentario().getTexto());

		return "success";
	}

	public String delete() {
		mgr.removeComentario(getComentario().getId().toString());
		addMessage("comentario.deleted", getComentario().getTexto());

		return "success";
	}
	
	// Convenience methods ====================================================
	public static String getRequestParameter(String name) {
		return (String) FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get(name);
	}

	public void addMessage(String key, String arg) {
		// sure is a lot of work to get the named ResourceBundle in JSF, eh?
		ApplicationFactory factory = (ApplicationFactory) FactoryFinder
				.getFactory(FactoryFinder.APPLICATION_FACTORY);
		String bundleName = factory.getApplication().getMessageBundle();
		ResourceBundle messages = ResourceBundle.getBundle(bundleName);

		// it's even more work to format a message with args
		MessageFormat form = new MessageFormat(messages.getString(key));

		String msg = form.format(new Object[] { arg });

		// add message to session so it can live past redirects
		// the MessageFilter class will take care of removing it
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		session.setAttribute("message", msg);
	}

	public void setUsuarioInSession(Usuario usuarioInSession) {
		this.usuarioInSession = usuarioInSession;
	}

	public Usuario getUsuarioInSession() {
		return usuarioInSession;
	}
}